package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Select extends JInternalFrame {
	Connection conn;
	
	DefaultTableModel model;//model객체선언
	int colCount;//칼럼갯수
	Update up;
	Input in;
	Delete de;
	
	DatabaseTest main;
	
	private JPanel panel;
	private JTextField inp;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;
	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Select frame = new Select();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Select(Connection c,DatabaseTest db) {
		super("조회",true,true,true,true);
		setBounds(100, 100, 487, 300);
		this.main=db;
		this.conn=c;
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
		
		inp.setText("select * from ptbl");
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getInp(), BorderLayout.CENTER);
			panel.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JTextField getInp() {
		if (inp == null) {
			inp = new JTextField();
			inp.setColumns(10);
		}
		return inp;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					columnadd();
				}
			});
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			
			//scrollPane.setViewportView(getTable());//테이블 만드는것	
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			
			table = new JTable(model);//칼럼 내용 추가하는 것
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					
					System.out.println("안녕");
					if (e.getButton() == 1) { // 원 클릭
						main.OpenUp();
						int a=table.getSelectedRow();
						up.code.setText(model.getValueAt(a, 0).toString());
						up.ea.setText(model.getValueAt(a, 1).toString());
						up.serial.setText(model.getValueAt(a, 2).toString());
						

						up.search1();//테이블값을 update로 보낸뒤 update에서 출력하는 것
						
						
						//up.del();//update에서 삭제하는 것
						
					}	
				}
			});
		}
		return table;
	}
	public void columnadd(){//<---요걸 추가해야 테이블 값을 초기화 할수 잇음
		String sql=inp.getText();
		
		if(table!=null){
			table=null;

			}
		
		try{
			PreparedStatement pstmt=conn.prepareStatement(sql);
			ResultSet rs=pstmt.executeQuery();
			
			ResultSetMetaData rsmd=null;//리졀트셋구조정보 갖는 객체
			rsmd=rs.getMetaData();//리쟐트셋에 대한 부가정보 갖는 객체얻어오기
			
			colCount=rsmd.getColumnCount();//칼럼갯수 얻어오기
			Vector<String> vec=new Vector<String>();//칼럼
			model=new DefaultTableModel(vec,0);
			for(int i= 1; i<=colCount; i++){
				
				String colName=rsmd.getColumnName(i);//i번재 위치의 칼럼명 얻어오기
				vec.add(colName);
			}
			
			while(rs.next()){
				Vector<String> vec2=new Vector<String>();//저장된내용 넣고 가져옴
				for(int i= 1; i<=colCount; i++){
					
					vec2.add(rs.getString(i));//vec2에 테이블저장된 값을 넣는다
				}
				model.addRow(vec2);//저장된 행을 출력
			}
			
			rs.close();
			pstmt.close();
				
		}catch(Exception ex){
			//ex.printStackTrace();
		}
		
		scrollPane.setViewportView(getTable());//테이블 만드는것
	}
	public void setU(Update u){ //update값을 받아옴
		this.up=u;
	}
//	public void setD(Delete d){
//		this.de=d;
//	}
//	public void setI(Input i){
//		this.in=i;
//	}
}

